package com.rwind.dynamic.controller;

import com.rwind.dynamic.common.MessageDto;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class MyController {



    @ResponseBody //关键返回json格式化数据
    public Object commonRequest(HttpServletRequest request, HttpServletResponse response){
        return MessageDto.getSuccess(getParams(request)) ;
    }

    public Map<String, String> getParams(HttpServletRequest request) {
        Map<String, String> params = new HashMap<String, String>();
        Enumeration<String> enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement().toString();
            params.put(name, delNull(request.getParameter(name)));
        }
        return params;
    }


    public static String delNull(String str) {
        if (str == null) {
            return "";
        } else if (str.trim().equals("null") || str.trim().equals("NULL")) {
            return "";
        } else {
            return str.trim();
        }
    }

}
